package com.cn.ccp.action.task;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.ClassScheduleDetailBean;
import com.cn.ccp.bean.TermSettingBean;
import com.cn.ccp.bean.UserBean;
import com.cn.ccp.util.CCPContants;
import com.cn.ccp.util.Contants;

public class TaskUserAction extends BaseAction {
	private Logger log = Logger.getLogger(TaskUserAction.class);
 
	private String strSchedule;//今天课程表
	private String strOline;//当前在线学习任务
	
	public TaskUserAction(){
		UserBean userBean = CCPContants.getUserBean();
    	log.info("userBean.getGrade() = "+userBean.getGrade());
    	if(userBean.getGrade() != null && !"".equalsIgnoreCase(userBean.getGrade()) &&  !"-1".equalsIgnoreCase(userBean.getGrade()))
    		initSchedule(userBean.getGrade());
    	initData();
	}
	
	public String getStrSchedule() {
		if(strSchedule == null || strSchedule.trim().length() == 0)
			strSchedule = getMessages("user_cur_task_title_no_shce");
		return strSchedule;
	}
	public String getStrOline() {
		return strOline;
	}
	
	private void initData(){
		int num = getSelectCourseService().onlineCount(String.valueOf(CCPContants.getUserBean().getId()));
		strOline = "您目前有["+num+"]门课程正在学习";
	}
	
	
	private void initSchedule(String gradeId){
		
		log.info("=============gradeId = "+gradeId);
		Calendar cur = Calendar.getInstance();
		int dayForWeek;
		if(cur.get(Calendar.DAY_OF_WEEK) == 1){  
			 dayForWeek = 7;  
		 }else{  
			 dayForWeek = cur.get(Calendar.DAY_OF_WEEK) - 1;  
		 }  
		int curYear = cur.get(Calendar.YEAR);
		List termSetList =  getTermSettingService().queryTerymByYear(curYear) ;
		if(termSetList == null){
			log.warn("Has been not setting class schudule..");
			return;
		}
		int count = termSetList.size();
		TermSettingBean termSettingBean ,curTermSettingBean = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for(int i =0; i<count; i++){
			termSettingBean = (TermSettingBean)termSetList.get(i);
			Calendar cal = Calendar.getInstance();
			try {
				cal.setTime(sdf.parse(termSettingBean.getFirstWeekStart()));
			} catch (ParseException e) {
				log.error("format data failed : ", e);
			}
			log.info(termSettingBean);
			int value = cal.compareTo(cur) ;
			log.info("------value = "+value);
			if(value == -1)//cal 比 cur 早
				curTermSettingBean = termSettingBean;
		}
		
		if(curTermSettingBean == null){
			log.warn("Has been not setting class schudule..");
			return ;
		}
		log.info("-------------"+curTermSettingBean.getId()+":"+gradeId);
		List<ClassScheduleDetailBean> allList = getClassScheduleDetailService().pageQueryForUserByGradeId(gradeId,curTermSettingBean.getId());//返回系统课程表
    	count = allList.size();
    	log.info("=============:count = "+count);
    	ClassScheduleDetailBean bean;
    	Map<String,Integer> dayMoningList = new HashMap<String,Integer>();//早上课程表list
    	Map<String,Integer> dayNoonList = new HashMap<String,Integer>();//上午课程表list
    	Map<String,Integer> dayAfterNoonList = new HashMap<String,Integer>();//下午课程表list
    	Map<String,Integer> dayNightList = new HashMap<String,Integer>();//晚上课程表list
    	Integer num = null;
    	log.info("=============:dayForWeek = "+dayForWeek);
    	for(int i=0; i<count; i++){
    		bean = allList.get(i);
    		if(bean.getSubjects().equalsIgnoreCase(Contants.SCHEDULE_CLASS_SUBJECT_INIT))
    			continue;
    		log.info(bean.getWeek()+ "----------"+bean.getSubjects());
    		if(bean.getWeek() == dayForWeek){
    			log.info("----------"+bean.getSubjects());
    			switch(bean.getScheduleType()){
    				case Contants.SCHEDULE_TYPE_MORNING://早上
    					num = dayMoningList.get(bean.getSubjects());
    					if(num == null)
    						dayMoningList.put(bean.getSubjects(), Integer.valueOf(1));
    					else
    						dayMoningList.put(bean.getSubjects(), num.intValue()+1);	
    					break;
    				case Contants.SCHEDULE_TYPE_FORENOON://上午
    					num = dayNoonList.get(bean.getSubjects());
    					if(num == null)
    						dayNoonList.put(bean.getSubjects(), Integer.valueOf(1));
    					else
    						dayNoonList.put(bean.getSubjects(), num.intValue()+1);	
    					break;
    				case Contants.SCHEDULE_TYPE_AFTERNOON://下午
    					num = dayAfterNoonList.get(bean.getSubjects());
    					if(num == null)
    						dayAfterNoonList.put(bean.getSubjects(), Integer.valueOf(1));
    					else
    						dayAfterNoonList.put(bean.getSubjects(), num.intValue()+1);	
    					break;
    				case Contants.SCHEDULE_TYPE_NIGHT://晚上
    					num = dayNightList.get(bean.getSubjects());
    					if(num == null)
    						dayNightList.put(bean.getSubjects(), Integer.valueOf(1));
    					else
    						dayNightList.put(bean.getSubjects(), num.intValue()+1);	
    					break;
    			}
    		}
    	}
    	
    	StringBuffer buf = new StringBuffer();
    	if(dayMoningList.size() > 0){
    		buf.append(getMessages("user_cur_task_class_schedule_morning"));
    		Iterator it = dayMoningList.entrySet().iterator();
    		while (it.hasNext()) {
	    		Map.Entry entry = (Map.Entry) it.next();
	    		Object key = entry.getKey();
	    		Object value = entry.getValue();
	    		buf.append(value+"节"+key+",");
    		} 
    	}
    	
    	 if(dayNoonList.size()> 0){
    		 buf.append(getMessages("user_cur_task_class_schedule_noon"));
     		Iterator it = dayNoonList.entrySet().iterator();
     		while (it.hasNext()) {
 	    		Map.Entry entry = (Map.Entry) it.next();
 	    		Object key = entry.getKey();
 	    		Object value = entry.getValue();
 	    		buf.append(value+"节"+key+",");
     		} 
    	 }
    	
    	 if(dayAfterNoonList.size()> 0){
    		 buf.append(getMessages("user_cur_task_class_schedule_afternoon"));
     		Iterator it = dayAfterNoonList.entrySet().iterator();
     		while (it.hasNext()) {
 	    		Map.Entry entry = (Map.Entry) it.next();
 	    		Object key = entry.getKey();
 	    		Object value = entry.getValue();
 	    		buf.append(value+"节"+key+",");
     		} 
    	 }
    	 
    	 if(dayNightList.size()> 0){
    		 buf.append(getMessages("user_cur_task_class_schedule_night"));
     		Iterator it = dayNightList.entrySet().iterator();
     		while (it.hasNext()) {
 	    		Map.Entry entry = (Map.Entry) it.next();
 	    		Object key = entry.getKey();
 	    		Object value = entry.getValue();
 	    		buf.append(value+"节"+key+",");
     		} 
    	 }
    	 
    	 strSchedule = buf.toString();
	}
	
	 
	
}
